package com.epalmpay.service.bonus.impl;

import com.epalmpay.dto.bonus.*;
import com.epalmpay.entity.BonusSendLog;
import com.epalmpay.entity.Member;
import com.epalmpay.entity.Page;
import com.epalmpay.enumdef.ActivityEnum;
import com.epalmpay.mapper.BonusSendLogMapper;
import com.epalmpay.mapper.MemberMapper;
import com.epalmpay.service.bonus.IBonusSendLogService;
import com.epalmpay.service.bonus.IBonusService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * Created by gwn on 2017/9/19.
 */
@Service("bonusSendLogService")
public class BonusSendLogServiceImpl implements IBonusSendLogService{

    @Resource
    private BonusSendLogMapper bonusSendLogMapper;

    @Resource
    private IBonusService bonusService;

    @Resource
    private MemberMapper memberMapper;


    @Override
    public BonusWarningListOutDTO queryBonusSendShopList(BonusWarningInDTO bonusWarningInDTO, Page page) throws Exception {

        BonusWarningListOutDTO bonusWarningListOutDTO = new BonusWarningListOutDTO();

        Map<String,Object> param=new HashMap<String,Object>();
        param.put("groupId",bonusWarningInDTO.getGroupId());
        param.put("industryId",bonusWarningInDTO.getIndustryId());
        param.put("shopName",bonusWarningInDTO.getShopName());
        param.put("adjustStatus",bonusWarningInDTO.getAdjustStatus());
        param.put("memberName",bonusWarningInDTO.getMemberName());
        param.put("cardNo",bonusWarningInDTO.getCardNo());
        param.put("joinStart",bonusWarningInDTO.getJoinStart());
        param.put("joinEnd",bonusWarningInDTO.getJoinEnd());



        int sumCount = bonusSendLogMapper.queryBonusWarningCount(param);
        page.setTotalResult(sumCount);
        if(sumCount<=0)
            return bonusWarningListOutDTO;

        param.put("limit", page.getShowCount());
        param.put("start", page.getShowCount()*(page.getCurrentPage()-1));


        List<BonusWarningOutDTO> bonusWarningOutDTOList = bonusSendLogMapper.queryBonusWarningList(param);
        for(BonusWarningOutDTO bonusWarningOutDTO:bonusWarningOutDTOList){
            bonusWarningOutDTO.setDiscountAmount(bonusWarningOutDTO.getBonusValue()+bonusWarningOutDTO.getDiscountAmount());
        }
        bonusWarningListOutDTO.setBonusWarningOutDTOList(bonusWarningOutDTOList);
        return bonusWarningListOutDTO;
    }

    @Override
    public void updateAdjustStatus(BonusWarningInDTO bonusWarningInDTO) throws Exception {
        BonusSendLog bonusSendLog = new BonusSendLog();
        bonusSendLog.setGroupId(bonusWarningInDTO.getGroupId());
        bonusSendLog.setGmtModified(new Date());
        bonusSendLog.setId(bonusWarningInDTO.getId());
        bonusSendLog.setAdjustStatus(ActivityEnum.BonusSendStatus.Accounted.getType());
        bonusSendLog.setIsFlag(ActivityEnum.BonusIsFlag.OnLine.getType());
        bonusSendLogMapper.updateByPrimaryKeySelective(bonusSendLog);

        BonusSendLog bonusSendLog1 = bonusSendLogMapper.queryBounsSendLogDetail(bonusSendLog);

        //给会员加积分 并在bonus_send_log 中插入一条已入账记录
//         bonusSendLog = BonusSendLog.build(bonusWarningInDTO.getGroupId(),bonusSendLog1.getMemberId(),
//                 bonusSendLog1.getSendType() , bonusSendLog1.getAdjustAmount(),ActivityEnum.BonusSendStatus.Accounted.getType(),bonusSendLog1.getId(),null,bonusSendLog1.getOrderId(),null);
//        bonusService.sendOneMemberBonus(bonusSendLog);
        //解冻积分入账
        int result = memberMapper.updateMemberBonusById(bonusSendLog1.getMemberId(),bonusSendLog1.getAdjustAmount());
        //更新会员冻结积分
         memberMapper.updateBonusFreezeById(bonusSendLog.getMemberId(),-1*bonusSendLog1.getAdjustAmount());

    }

    @Override
    public void updateAdjustStatusFail(BonusWarningInDTO bonusWarningInDTO) throws Exception {
        BonusSendLog bonusSendLog = new BonusSendLog();
        bonusSendLog.setGroupId(bonusWarningInDTO.getGroupId());
        bonusSendLog.setGmtModified(new Date());
        bonusSendLog.setId(bonusWarningInDTO.getId());
        bonusSendLog.setAdjustStatus(ActivityEnum.BonusSendStatus.Invalided.getType());
        bonusSendLogMapper.updateByPrimaryKeySelective(bonusSendLog);

        BonusSendLog bonusSendLog1 = bonusSendLogMapper.queryBounsSendLogDetail(bonusSendLog);
        //更新会员冻结积分
        memberMapper.updateBonusFreezeById(bonusSendLog1.getMemberId(),-1*bonusSendLog1.getAdjustAmount());

    }

    @Override
    public BonusWarningOutDTO querybonusSendLogDetail(BonusWarningInDTO bonusWarningInDTO) {


        return bonusSendLogMapper.querybonusSendLogDetail(bonusWarningInDTO);
    }


    /**
     * 根据当前选中项,更新状态,然后标记更新成功标志或者失败标志,存放列表
     * @param bonusWarningInDTOList
     * @return
     */

    @Override
    public List<Integer> updateAdjustStatus(List<BonusWarningInDTO> bonusWarningInDTOList)  {
        List<Integer> list = new ArrayList<Integer>();

        try {

            for (int i = 0; i<bonusWarningInDTOList.size(); i++){
                BonusSendLog bonusSendLog = new BonusSendLog();
                bonusSendLog.setGroupId(bonusWarningInDTOList.get(i).getGroupId());
                bonusSendLog.setId(bonusWarningInDTOList.get(i).getId());
                bonusSendLog.setAdjustStatus(bonusWarningInDTOList.get(i).getAdjustStatus());
                bonusSendLog.setIsFlag(ActivityEnum.BonusIsFlag.OnLine.getType());
                BonusSendLog bonusSendLog1 = bonusSendLogMapper.queryBounsSendLogDetail(bonusSendLog);
                if(bonusSendLog1.getAdjustStatus()==1){
                    list.add(bonusSendLogMapper.updateByPrimaryKeySelective(bonusSendLog));
                    if(bonusWarningInDTOList.get(i).getAdjustStatus()==3){
                        //解冻积分入账
                        Member member =memberMapper.queryBonusAmount(bonusSendLog1.getMemberId());
                        member.setBonusAmount(member.getBonusAmount()+bonusSendLog1.getAdjustAmount());
                        memberMapper.updateByPrimaryKeySelective(member);

                        //更新会员冻结积分
                        memberMapper.updateBonusFreezeById(bonusSendLog1.getMemberId(),-1*bonusSendLog1.getAdjustAmount());
                    }
                    else{//作废
                        //更新会员冻结积分
                        memberMapper.updateBonusFreezeById(bonusSendLog1.getMemberId(),-1*bonusSendLog1.getAdjustAmount());
                    }
                }

            }
        }catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    /**
     * 积分发放管理
     */

    @Override
    public BonusSendLogListOutDTO selectBonusSendList(BonusSendLogInDTO bonusSendLogInDTO, Page page) throws Exception {
        BonusSendLogListOutDTO bonusSendLogListOutDTO = new BonusSendLogListOutDTO();

        Map<String,Object> param=new HashMap<String,Object>();
        param.put("groupId",bonusSendLogInDTO.getGroupId());
        param.put("industryId",bonusSendLogInDTO.getIndustryId());
        param.put("shopName",bonusSendLogInDTO.getShopName());
        param.put("adjustStatus",bonusSendLogInDTO.getAdjustStatus());
        param.put("sendType",bonusSendLogInDTO.getSendType());
        param.put("joinStart",bonusSendLogInDTO.getJoinStart());
        param.put("joinEnd",bonusSendLogInDTO.getJoinEnd());



        int sumCount = bonusSendLogMapper.selectBonusSendLogCount(param);
        page.setTotalResult(sumCount);
        if(sumCount<=0)
            return bonusSendLogListOutDTO;

        param.put("limit", page.getShowCount());
        param.put("start", page.getShowCount()*(page.getCurrentPage()-1));

        List<BonusSendLogOutDTO> bonusSendLogOutDTOList = bonusSendLogMapper.selectBonusSendLogList(param);
        bonusSendLogListOutDTO.setBonusSendLogOutDTOList(bonusSendLogOutDTOList);
        return bonusSendLogListOutDTO;
    }

    @Override
    public BonusSendLogOutDTO queryDetail(BonusSendLogInDTO bonusSendLogInDTO) throws Exception {
        return bonusSendLogMapper.queryDetail(bonusSendLogInDTO);
    }

    @Override
    public BonusWarningOutDTO queryBonusData(BonusWarningInDTO bonusWarningInDTO) throws Exception {
        return bonusSendLogMapper.queryBonusData(bonusWarningInDTO);
    }

    @Override
    public int queryBonusCount(BonusWarningInDTO bonusWarningInDTO) {
        return bonusSendLogMapper.queryBonusCount(bonusWarningInDTO);
    }

    @Override
    public void updateAdjustStatusAll(BonusWarningInDTO bonusWarningInDTO) throws Exception {

        List<BonusSendLog> bonusSendLogList = bonusSendLogMapper.queryBonusList(bonusWarningInDTO);

        Integer amount=0;
        for(BonusSendLog bonusSendLog:bonusSendLogList){

            BonusSendLog bonusSendLog1 = new BonusSendLog();
            bonusSendLog1.setGmtModified(new Date());
            bonusSendLog1.setId(bonusSendLog.getId());
            bonusSendLog1.setAdjustStatus(ActivityEnum.BonusSendStatus.Accounted.getType());

             amount += bonusSendLog.getAdjustAmount();
            bonusSendLogMapper.updateByPrimaryKeySelective(bonusSendLog1);
        }


        //给会员加积分 并在bonus_send_log 中插入一条已入账记录

        //解冻积分入账
        int result = memberMapper.updateMemberBonusById(bonusWarningInDTO.getMemberId(),amount);

        //更新会员冻结积分
        memberMapper.updateBonusFreezeById(bonusWarningInDTO.getMemberId(),-1*amount);
    }

}
